home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / CENVIW1.ZIP / CMMTUTOR.DOC < prev    next >
Text File  |  1993-08-10  |  47KB  |  1,095 lines

  1.                     CEnvi Shareware Manual, Chapter 2:
  2.                            Cmm Language Tutorial
  3.  
  4.  
  5.                       CEnvi unregistered version 1.0
  6.                               10 AUGUST 1993
  7.  
  8.                        CEnvi Shareware User's Manual
  9.  
  10.           Copyright 1993, Nombas, All Rights Reserved.
  11.           Published by Nombas, P.O. Box 875, Medford, MA 02155 USA
  12.           (617)391-6595, (617)391-5289
  13.  
  14.           Thank you for trying this shareware version of CEnvi from Nombas.
  15.  
  16. 2.  The Cmm Language: Tutorial for Non-C Programmers
  17.  
  18.           The information in this chapter is geared toward those who are
  19.           not familiar with the C programming language.  C programmers
  20.           should jump ahead to the next chapter: Cmm versus C.  This
  21.           section is an introduction to and description of the Cmm
  22.           programming language.
  23.  
  24.           If you can write a batch, script, or macro file, or if you can
  25.           remember what "y = x + 1" means from your algebra class, then
  26.           you're ready to take on Cmm.  Really.  Cmm contains only
  27.           variables, mathematics symbols (remember algebra), and these few
  28.           statements: IF, ELSE, DO, WHILE, FOR, SWITCH, CASE, BREAK,
  29.           DEFAULT, CONTINUE, GOTO, and RETURN.
  30.  
  31.           This section is an abbreviation of the Cmm Language Tutorial
  32.           chapter in the CEnvi registered manual.  The CEnvi registered
  33.           manual goes into much more depth, has many more examples, and
  34.           follows a step-by-step tutorial to create a simple text editor
  35.           with CEnvi.
  36.  
  37. 2.1.  Your first Cmm program
  38.  
  39.           Before going into a description of Cmm, let's first make sure
  40.           that CEnvi is working properly.  With a text editor (e.g., EDIT
  41.           for DOS, E for OS/2, or NOTEPAD for Windows) create the file
  42.           HELLO.CMM and enter this text:
  43.  
  44.               // Hello.cmm: My first Cmm program
  45.               Count = 1; /* Count is how many Cmm programs I've written */
  46.               printf("Hello world. This is my %dst Cmm program.\n",Count);
  47.               printf("Press any key to quit...");
  48.               getch();
  49.  
  50.           You have now written a Cmm program named "HELLO.CMM".  Don't be
  51.           concerned if you do not yet understand the HELLO.CMM program; it
  52.           should become clear as Cmm is defined.  Now execute this program
  53.           (for DOS or OS/2 you would enter "CENVI HELLO.CMM" and for
  54.           Windows you need may use the File Manager and double-click on the
  55.           file name).  You should get this output:
  56.  
  57.               Hello world. This is my 1st Cmm program.
  58.               Press any key to quit...
  59.  
  60.  
  61.           If this program will execute, then you are ready to go on to
  62.           learn about Cmm.  If it did not execute then consult the CEnvi
  63.           installation section in the first chapter of this shareware
  64.           manual.
  65.  
  66. 2.2.  Cmm comments
  67.  
  68.           Comments are used in Cmm code to explain what the code does, but
  69.           the comment itself does nothing.  Comments are very useful in
  70.           programming.  A comment takes nothing away from the execution of
  71.           a program, but adds immeasurably to the readability of the source
  72.           code.
  73.  
  74.           In Cmm, any text on a line following two slash characters (//) is
  75.           considered a comment and so is ignored by the Cmm interpreter.
  76.           Likewise, anything between a slash-asterisk (/*) and an
  77.           asterisk-slash (*/) is a comment (this type of comment may extend
  78.           over many lines).  In the HELLO.CMM program there is a "//"
  79.           comment on the first line and a "/* blah blah */" comment on the
  80.           second line.
  81.  
  82. 2.3.  Cmm primary data types
  83.  
  84.           There are three principal data types in Cmm:
  85.             *Byte: A character (e.g., 'D') or a whole number between 0 and
  86.               255, inclusive
  87.             *Integer: A whole number value; this is the most common numeric
  88.               data type (examples: 0, -1000, 567, 4335600)
  89.             *Float: floating point numbers; any number containing a decimal
  90.               point (examples: 0.567, 3.14159, -5.456e-12)
  91.  
  92.           Cmm determines the data type of a number by how it is used; for
  93.           example, in the HELLO.CMM program the "1" in the second line is
  94.           an integer because that is the default type for numbers without a
  95.           decimal point.
  96.  
  97. 2.4.  Cmm Variables
  98.  
  99.           A Cmm variable is a symbol that may be assigned data.  The
  100.           assignment of data is usually performed by the equal sign (=), as
  101.           in the line "Count = 1" in HELLO.CMM.  After variables have been
  102.           assigned, they can be treated as their data type.  So, after
  103.           these statements:
  104.               Count = 1               // assign count as the integer 1
  105.               Count = Count + 2       // same as: Count = 1 + 2
  106.           Count would now have the value 3.
  107.  
  108. 2.5.  Cmm Expressions, Statements, and Blocks
  109.  
  110.           A Cmm "expression" or "statement" is any sequence of code that
  111.           perform a computation or take an action (e.g., "Count=1",
  112.           "(2+4)*3").  Cmm code is executed one statement at a time in the
  113.           order it is read.  A Cmm program is a series of statements
  114.           executed sequentially, one at a time.  Each line of HELLO.CMM,
  115.           for example, follows the previous line as it is written and as it
  116.           is executed.
  117.  
  118.           A statement usually ends in a semicolon (;) (this is required in
  119.           C, and still a good idea in Cmm to improve readability).  Each
  120.           program statement is usually written on a separate line to make
  121.           the code easy to read.
  122.  
  123.           Expressions may be grouped to effect the sequence of processing;
  124.           expressions inside parentheses are processed first.  Notice that:
  125.               4 * 7 - 5 * 3       // 28 - 14 = 13
  126.           has the same meaning, do to algebraic operator precedence, as:
  127.               (4 * 7) - (5 * 3)   // 28 - 15 = 13
  128.           but has a different meaning than:
  129.               4 * (7 - 5) * 3     // 4 * 2 * 3 = 8 * 3 = 24
  130.           which is still different from:
  131.               4 * (7 - (5 * 3))   // 4 * (7 - 15) = 4 * -8 = -32
  132.  
  133.           A "block" is a group of statements enclosed in curly braces ({})
  134.           to show that they are all a group and so are treated as one
  135.           statement.  For example, HELLO.CMM may be rewritten as:
  136.               // Hello.cmm: My first Cmm program
  137.               Count = 1; /* Count is how many Cmm programs I've written */
  138.               printf("Hello world. This is my %dst Cmm program.\n",Count);
  139.               {
  140.                 // this block tells the user we're done, and quits
  141.                 printf("Press any key to quit...");
  142.                 getch();
  143.               }
  144.           The indentation of statements is not necessary, but is useful for
  145.           readability.
  146.  
  147. 2.6.  Cmm Mathematical Operators
  148.  
  149.           Cmm code usually contains some mathematical operations, such as
  150.           adding numbers together, multiplying, dividing, etc.  These are
  151.           written in a natural way, such as "2 + 3" when you want to add
  152.           two and three.  The next few subsections define the recognized
  153.           operators within the comments of sample code:
  154.  
  155. 2.6.1   Basic Arithmetic
  156.  
  157.               //  "="  assignment: sets a variable's value
  158.               i = 2;      // i is 2
  159.  
  160.               //  "+"  addition: adds two numbers
  161.               i = i + 3;  // i is now (2+3) or 5
  162.  
  163.               //  "-"  subtraction: subtracts a number
  164.               i = i - 3;  // i is now (5-3) or 2 again
  165.  
  166.               //  "*"  multiplication:
  167.               i = i * 5;  // i is now (2*5) or 10
  168.  
  169.               //  "/"  division:
  170.               i = i / 3;  // i is now (10/3) or 3 (no remainder)
  171.  
  172.               //  "%"  remainder: remainder after division
  173.               i = 10;
  174.               i = i % 3;  // i is now (10%3) or 1
  175.  
  176. 2.6.2   Assignment Arithmetic
  177.  
  178.           Each of the above operators can comb